package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.ThreadLocalUtils;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.ex.CustomerException;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 购物车业务层实现
 */
@Service
@Transactional
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {


    /**
     * 加入菜品或者套餐到购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public R add(ShoppingCart shoppingCart) {
        // 获取登录用户ID
        Long user = ThreadLocalUtils.get();
        if (user == null) {
            throw new CustomerException("为登录，请登录后继续点餐");
        }
        shoppingCart.setUserId(user);

        LambdaQueryWrapper<ShoppingCart> lqw = Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, user);

        // 判断加入的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 根据用户ID和加入的菜或者套餐查询购物车对象
        ShoppingCart cart = this.getOne(lqw);

        // 判断购物车对象是否为null
        if (cart == null) {
            // 为空，没有加过这个菜或者套餐
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            cart = shoppingCart;
        } else {
            // 不是空得，已经加过，数量+1
            cart.setNumber(cart.getNumber() + 1);
            cart.setCreateTime(LocalDateTime.now());
            // 更新回数据库
            this.updateById(cart);
        }

        // 把最新得购物车数据返回
        return R.success(cart);
    }

    /**
     * 减少菜品数量
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public R sub(ShoppingCart shoppingCart) {
        // 获取登录用户ID
        Long user = ThreadLocalUtils.get();
        if (user == null) {
            throw new CustomerException("为登录，请登录后继续点餐");
        }
        shoppingCart.setUserId(user);

        LambdaQueryWrapper<ShoppingCart> lqw = Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, user);

        // 判断减少的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 根据用户ID和减少的菜或者套餐查询购物车对象
        ShoppingCart cart = this.getOne(lqw);

        if (cart == null) {
            throw new CustomerException("购物车已清空");
        }
        if (cart.getNumber() <= 1) {
            this.removeById(cart);
            cart = new ShoppingCart();
        } else {
            cart.setNumber(cart.getNumber() - 1);
            cart.setCreateTime(LocalDateTime.now());
            this.updateById(cart);
        }
        return R.success(cart);
    }
}